 /** \file spi.h
  **
  **  file
  **
  ** History:
  **   - Wednesday, November 06, 2013  1.0  Mark  First Version
  *************************************************************************/

 #ifndef __SPI_H__
 #define __SPI_H__

 /*****************************************************************************/
 /* Include files                                                             */
 /*****************************************************************************/
//#include "includes.h"
#include "mcu.h"
#include "base_types_l3.h"
 /*****************************************************************************/
 /* Local pre-processor symbols/macros ('#define')                            */
 /*****************************************************************************/
#ifndef CLOCK_FREQ
    #ifdef __HCLK
        #define CLOCK_FREQ __HCLK>>APBC0_PSR_Val
    #else
        #define CLOCK_FREQ 40000000UL
    #endif
#endif

#define SPI_BAUDRATE(x)  (uint16_t)((CLOCK_FREQ+x/2)/(uint32_t)x - 1)

#define HIGH	1
#define LOW		0
#define OUTPUT	1
#define INPUT	0
#define SPI_CS_MASK		(1<<0)|(1<<1)|(1<<2)|(1<<3)
#define SPI_CS_DDR		FM3_GPIO->DDR3
#define SPI_CS_PFR		FM3_GPIO->PFR3

#define SPI_CS1_DDR		bFM3_GPIO_DDR3_P0
#define SPI_CS2_DDR		bFM3_GPIO_DDR3_P1
#define SPI_CS3_DDR		bFM3_GPIO_DDR3_P2
#define SPI_CS4_DDR		bFM3_GPIO_DDR3_P3

#define SPI_MASTER_CS1	bFM3_GPIO_PDOR3_P0
#define SPI_MASTER_CS2	bFM3_GPIO_PDOR3_P1
#define SPI_MASTER_CS3	bFM3_GPIO_PDOR3_P2
#define SPI_MASTER_CS4	bFM3_GPIO_PDOR3_P3

#define SPI_SALVE_CS1	bFM3_GPIO_PDIR3_P0
#define SPI_SALVE_CS2	bFM3_GPIO_PDIR3_P1
#define SPI_SALVE_CS3	bFM3_GPIO_PDIR3_P2
#define SPI_SALVE_CS4	bFM3_GPIO_PDIR3_P3
 /*****************************************************************************/
 /* Global variable definitions (declared in header file with 'extern')       */
 /*****************************************************************************/
 /*****************************************************************************/
 /* Local type definitions ('typedef')                                        */
 /*****************************************************************************/

 /*****************************************************************************/
 /* Local variable definitions ('static')                                     */
 /*****************************************************************************/
boolean_t Spi_Master_Init(uint8_t u8Spi, uint32_t Baudrate);
boolean_t Spi_Slave_Init(uint8_t u8Spi);
boolean_t Spi_Deinit(uint8_t u8Spi);

void SpiWriteByte(uint8_t u8Spi, uint8_t u8Data);
void SpiWriteBuffer(uint8_t u8Spi, uint8_t* buff, uint8_t buffsize);
uint8_t SpiReadByte(uint8_t u8Spi);
void SpiRxIrqHandler(uint8_t u8Spi);

 #endif
